<?php
/*
仓库资料
*/
// declare(strict_types=1);

namespace Modules\Warehouse\Http\Controllers;

use Catch\Base\CatchController as Controller;
// use Modules\Warehouse\Models\Departments;
use Illuminate\Http\Request;
use extend\services\warehouse\{warehouseService};//引入自定义类
use Illuminate\Support\Facades\Cache;
use Modules\Warehouse\Models\Stash;
use Catch\Exceptions\FailedException;


class DatatypeController extends Controller
{
    public function __construct(
        protected readonly Stash $model
    ){}
 

    /**
     * @return mixed
     */
    public function index(Request $request)
    {   


        $data = $this->model->getList();
        return $data;
        // $kingDeeService = new warehouseService();
        // $data = $kingDeeService->warehousedata($request->all());

    }
    
    /**
     *仓库接口
     */
    // public function warehouseApi(Request $request)
    // {   
    //     $kingDeeService = new warehouseService();
    //     $data = $kingDeeService->warehousedata($request->all());
    //     $resdata = [];
    //     foreach($data as $k=>$v){
    //         $resdata[$k]['label'] = $v['FName'];
    //         $resdata[$k]['value'] = $v['FSTOCKID'];
    //     }
    //     return $resdata;
    // }

    /**
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request)
    {
        $data = $request->all();
        $stash = $this->model->select('fnumber')
                ->where('fnumber',$data['fnumber'])
                ->first();
                // ->toArray();
        if(!empty($stash)){
            throw new FailedException('仓库编码不能重复');
        }
        
        return $this->model->storeBy($request->all());
    }

    /**
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function update($id, Request $request)
    {
        $stash = $this->model->select('fmaterial','id')
                ->where('fmaterial',$id)
                ->first();
                // ->toArray();
                
        if(empty($stash)){
            throw new FailedException('仓库编码不在');
        }
        return $this->model->updateBy($stash['id'], $request->all());
    }

    /**
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
        $stash = $this->model->select('fmaterial','id')
                ->where('fmaterial',$id)
                ->first();
                // ->toArray();
        return $this->model->deleteBy($stash['id']);
    }
   
    
    /**
     *仓库打印接口
     */
    public function Printinterface(Request $request)
    {   

        $data = $request->all();
        $kingDeeService = new warehouseService();
        
        $data = $this->model->select('fmaterial','fnumber','fname','fuseorgid_number','fuseorgid_name')
            ->whereIn('id',$data['id'])
            ->get()
            ->toArray();

        foreach($data as $k=>$v){
            $fnumber = $kingDeeService->createQrCode('100',$v['fnumber'],$v['fnumber']);
            $data[$k]['Twocode']  = url()->previous().'/uploads/barcode/'.$v['fnumber'].'.png';
        }       
        return ['code'=>1000,'message'=>'添加成功','data'=>$data];
    }


   

    
  


}
